<template>
  <div class="orders">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <div slot="header">
        <el-input placeholder="请输入内容" clearable>
          <el-button slot="append" icon="el-icon-search"></el-button>
        </el-input>
      </div>
      <el-table :data="orderList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="订单编号" prop="order_number"></el-table-column>
        <el-table-column label="订单价格" prop="order_price"></el-table-column>
        <el-table-column label="是否付款" prop="pay_status">
          <template #default="{ row }">
            <el-tag type="success" v-if="(row.pay_status = '1')">已付款</el-tag>
            <el-tag type="danger" v-else>未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否发货" prop="is_send"></el-table-column>
        <el-table-column label="下单时间" prop="create_time">
          <template #default="{ row }">
            {{ row.create_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button
              icon="el-icon-edit"
              type="primary"
              size="mini"
              @click="editOrder(row.order_id)"
            ></el-button>
            <el-button
              icon="el-icon-location-outline"
              type="success"
              size="mini"
              @click="showProgress()"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="queryInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
    <el-dialog
      title="修改订单数据"
      :visible.sync="orderDialogVisible"
      width="50%"
    >
      <el-form ref="orderForm">
        <el-form-item label="订单价格">
          <el-input v-model="orderForm.order_price" type="number"></el-input>
        </el-form-item>
        <el-form-item label="是否付款">
          <el-radio v-model="orderForm.pay_status" label="1">已付款</el-radio>
          <el-radio v-model="orderForm.pay_status" label="0">未付款</el-radio>
        </el-form-item>
        <el-form-item label="是否发货">
          <el-radio v-model="orderForm.is_send" label="1">是</el-radio>
          <el-radio v-model="orderForm.is_send" label="0">否</el-radio>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="orderDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="subOrder">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog
      title="物流进度"
      :visible.sync="showProgressDialogVisible"
      width="50%"
    >
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in progressInfo"
          :key="index"
          :timestamp="activity.time"
        >
          {{ activity.context }}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import {
  getOrdersListApi,
  getOrderApi,
  subOrderApi,
  getProgressInfoApi,
} from "@/api/user";
export default {
  name: "OrdersIndex",
  data() {
    return {
      // 查询参数
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
      total: 0,
      orderList: [],
      orderDialogVisible: false,
      orderForm: {
        order_price: 0,
        pay_status: "1",
        is_send: "1",
      },
      order_id: "",
      showProgressDialogVisible: false,
      progressInfo: [],
    };
  },
  created() {
    this.getOrdersList();
  },
  methods: {
    async getOrdersList() {
      const res = await getOrdersListApi(this.queryInfo);
      // console.log(res);
      this.orderList = res.data.goods;
      this.total = res.data.total;
      if (res.meta.status !== 200) {
        return this.$message.error("获取订单列表失败！");
      }
    },
    handleSizeChange(currentSize) {
      // console.log(currentSize);
      this.queryInfo.pagesize = currentSize;
      this.getOrdersList();
    },
    handleCurrentChange(currentPage) {
      // console.log(currentPage);
      this.queryInfo.pagenum = currentPage;
      this.getOrdersList();
    },
    async editOrder(id) {
      this.order_id = id;
      const res = await getOrderApi(id);
      // console.log(res);
      this.orderForm.order_price = res.data.order_price;
      this.orderForm.pay_status = res.data.pay_status;
      this.orderForm.is_send = res.data.is_send === "否" ? "0" : "1";
      this.orderDialogVisible = true;
    },
    async subOrder() {
      await subOrderApi(this.order_id, this.orderForm);
      this.getOrdersList();
      this.$message.success("修改成功");
      this.orderDialogVisible = false;
    },
    async showProgress() {
      this.showProgressDialogVisible = true;
      const res = await getProgressInfoApi(1106975712662);
      // console.log(res);
      if (res.meta.status !== 200) {
        return this.$message.error("获取物流进度失败！");
      }
      this.progressInfo = res.data;
    },
  },
};
</script>

<style lang="scss" scoped>
.orders {
  .el-breadcrumb {
    margin-bottom: 15px;
  }
  .el-card {
    .el-input {
      width: 33%;
      margin-right: 20px;
    }
    .el-table {
      margin-bottom: 15px;
    }
  }
}
</style>
